package com.code4j.ai.mcp.server.filter;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;
@Component
public class AuthorizationFilter extends OncePerRequestFilter
{
    private static final ThreadLocal<String> currentAuthHeader = new ThreadLocal<>();

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
            throws ServletException, IOException {
        try {
            String authHeader = request.getHeader("Authorization");
            currentAuthHeader.set(authHeader);
            chain.doFilter(request, response);
        } finally {
            currentAuthHeader.remove();
        }
    }

    public static String getCurrentAuthHeader() {
        return currentAuthHeader.get();
    }
}